using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace MyUtil
{
    class TimerManager : MonoBehaviour
    {
        private static TimerManager instance;
        public static TimerManager Instance => instance;
        
        List<Timer> timers = new();

        private void Awake()
        {
            instance = this;            
        }

        public void Update()
        {        
            foreach(Timer e in timers)
            {   
                if(e==null)
                {
                    timers.Remove(e);
                    continue;
                }
                e.Update(Time.deltaTime);
            }
        }

        public void RemoveTimer(Timer timer)
        {
            timers.Remove(timer);
        }

        public void RegisterTimer(Timer timer)
        {
            timers.Add(timer);
        }

        public Timer CreateTimer(float waitTime,Action callBack,bool oneShot=true)
        {
            Timer timer = new Timer(waitTime,callBack,oneShot);
            timers.Add(timer);
            return timer;
        }
    
    }
}